#if defined(__WIN__)
#include <process.h>
#elif defined(__LINUX__)
#define INFINITE -1
#endif

#include "../h/iThread.h"
#include "../h/iThreadMgr.h"
#define INVALID_THREAD NULL

TLS iThread* iThread::tls_thread = NULL;

iThread::iThread()
:m_runing(true)
,m_thread(INVALID_THREAD)
{

}
iThread::~iThread()
{

}
long iThread::get_id()
{
	return m_id;
}
uint32_t iThread::wait(uint32_t ms)
{
	return m_event.wait(ms);
}
int iThread::start()
{
	m_event.reset();
#if defined(__WIN__)
	m_thread = _beginthread(thread_run, 0, this);
#elif defined(__LINUX__)
    pthread_create(&m_thread,  NULL, &thread_run, this);
#endif
	m_event.wait(INFINITE);
	return 0;
}
int iThread::stop(uint32_t ms)
{
	m_runing = false;
	m_event.wait(ms);
	m_thread = NULL;
	return 0;
}
#if defined(__WIN__)
void iThread::thread_run(void* param)
#elif defined(__LINUX__)
void* iThread::thread_run(void* param)
#endif
{
	iThread* pThis = (iThread*)param;
	tls_thread = pThis;
	pThis->m_id = iThreadMgr::get_thread_id();
	pThis->m_event.signal();
	pThis->run();

    #if defined(__LINUX__)
    return NULL;
    #endif
}
